package com.example.tanhuanapp.controller;


import com.example.tanhuanapp.pojo.vo.ErrorResult;
import com.example.tanhuanapp.server.ShortNoteServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @author IT空门_门主
 * @date 2024/1/8
 * 获取验证码
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(tags = "获取验证码")
public class SmsController {
    @Autowired
    private ShortNoteServer shortNoteServer;

    @PostMapping("login")
    @ApiOperation(value = "获取验证码")
    public ResponseEntity<Object> sendCheckCode(@RequestBody Map<String,Object> param){

        ErrorResult.ErrorResultBuilder resultBuilder =
                ErrorResult.builder().errCode("000000").errMessage("短信发送失败");

        //获取手机号
        Map<String, Object> sendCheckCode=shortNoteServer.sendCheckCode((String) param.get("phone"));
        //获取code
        int code= ((Integer) sendCheckCode.get("code")).intValue();
        log.info("code:{}",code);
        if (code==3){
            return ResponseEntity.ok(null);
        }else if (code==1){
            log.info("短信发送1:{}",sendCheckCode.get("msg").toString());
            resultBuilder.errCode("000001").errMessage(sendCheckCode.get("msg").toString());
        }else if (code==2){
            log.info("短信发送失败");
            resultBuilder.errCode("500").errMessage("短信发送失败");
        }
        return ResponseEntity.status(500).body(resultBuilder.
                        build());
    }
}
